const BASE_URL = "https://demo.supconit.net"
const BASE_URL_MC = BASE_URL + '/prod-api/mc-config/model/device'

class Request {
  constructor(baseURL, authHeader = {}) {
    this.baseURL = baseURL
    this.authHeader = authHeader
  }
  /**
   * 数据请求 
   * @param {string} url 请求路径
   * @param {*} method 请求方法
   * @param {Object} params 请求参数
   * @param {*} isAuth 是否需要验证
   * @param {*} header 请求头
   * @returns 
   */
  request(url, method, params, isAuth = false, header = {}) {
    const finalHeader = isAuth ? { ...this.authHeader, ...header } : header
    return new Promise((resolve, reject) => {
      uni.request({
        url: this.baseURL + url,
        method: method,
        data: params,
        header: finalHeader,
        success(res) {
          resolve(res.data)
        },
        fail: reject
      })
    })
  }
  get(url, params, isAuth = true, header) {
    return this.request(url, 'GET', params, isAuth, header)
  }
  post(url, data, isAuth = true, header) {
    return this.request(url, 'POST', data, isAuth, header)
  }
}

const McRequest = new Request(BASE_URL_MC, {
  Authorization: 'Bearer ' + uni.getStorageSync('token'),
})


export function getList(params) {
  return request.get("/list", params)
}

// 获取mc列表
export function getMcList({ typeId, page = 1, size = 20, order = '_id', asc = false, useCode = true }, data = []) {
  // 请求路径
  let url = `/page/${typeId}?size=${size}&page=${page}&order=${order}&asc=${asc}&useCode=${useCode}` 
  return McRequest.post(url, data)
}

// 往mc列表中添加一条数据
export function addMcData(tableID, data) {
  return McRequest.post('/' + tableID, data)
}